package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hxkcloud.patient.R;
import com.hxkcloud.patient.activitys.DisplayImageActivity;
import com.hxkcloud.patient.beans.DischargeSummaryBean;
import com.hxkcloud.patient.beans.ImageBean;
import com.hxkcloud.patient.utils.ResetListViewCount;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 出院小结
 * Created by Administrator on 2017/1/12.
 */

public class DischargeSummaryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<DischargeSummaryBean> dischaegeBeanList;
    private Context mContext;
    private LayoutInflater inflater;

    private OnItemClickListener onItemClickListener;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public DischargeSummaryAdapter(List<DischargeSummaryBean> pushRecordBeanList, Context mContext) {
        this.dischaegeBeanList = pushRecordBeanList;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //adapter_discharge_summary
        View view = inflater.inflate(R.layout.adapter_discharge_summary, null);
        DischargeSummaryHolder dischargeSummaryHolder = new DischargeSummaryHolder(view);
        return dischargeSummaryHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        DischargeSummaryHolder dischargeSummaryHolder = (DischargeSummaryHolder) holder;
        //dischargeSummaryHolder.recyclerview_image.setLayoutManager(new GridLayoutManager(mContext, 3));
        List<ImageBean> imageBeanList = new ArrayList<>();
        if (dischaegeBeanList.get(position).getImages() != null)
            for (int i = 0; i < dischaegeBeanList.get(position).getImages().size(); i++) {
                ImageBean imageBean = new ImageBean();
                imageBean.setImageType(2);
                imageBean.setImage(dischaegeBeanList.get(position).getImages().get(i));
                imageBeanList.add(imageBean);
            }
        final ImageSupGridAdapter imageAdapter = new ImageSupGridAdapter(imageBeanList, mContext);
        imageAdapter.setOnItemClickListener(new ImageSupGridAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(mContext, DisplayImageActivity.class);
                intent.putExtra("path", imageAdapter.getImageBeen(position).getImage());

                intent.putExtra("title", (position + 1) + "/" + imageAdapter.getCount());
                intent.putExtra("position", position);
                intent.putStringArrayListExtra("pics", (ArrayList<String>) imageAdapter.getImageBeen());
                intent.putExtra("isHideDelete", "true");
                mContext.startActivity(intent);
            }
        });
        dischargeSummaryHolder.grid_image.setAdapter(imageAdapter);
        ResetListViewCount.setGridViewHeightBasedOnChildren(dischargeSummaryHolder.grid_image);

        //dischargeSummaryHolder.recyclerview_image.add
        dischargeSummaryHolder.tv_content.setText(dischaegeBeanList.get(position).getSummary());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date(dischaegeBeanList.get(position).getDischargeTime()));
        dischargeSummaryHolder.tv_time.setText(date);
        dischargeSummaryHolder.tv_edit.setTag(position);
        dischargeSummaryHolder.tv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int index = (int) v.getTag();
                if (onItemClickListener != null) {
                    onItemClickListener.onClick(index);
                }

            }
        });
    }


    @Override
    public int getItemCount() {
        return dischaegeBeanList.size();
    }

    /**
     * 每一个位置的item都作为单独一项来设置
     * viewType 设置为position
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {

        return position;
    }

    class DischargeSummaryHolder extends RecyclerView.ViewHolder {
        public TextView tv_content;
        public TextView tv_time;
        public LinearLayout line_click;
        public TextView tv_edit;
        public GridView grid_image;

        public DischargeSummaryHolder(View itemView) {
            super(itemView);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            line_click = (LinearLayout) itemView.findViewById(R.id.line_click);
            tv_edit = (TextView) itemView.findViewById(R.id.tv_edit);
            grid_image = (GridView) itemView.findViewById(R.id.grid_image);
        }
    }

    public interface OnItemClickListener {

        void onClick(int position);
    }
}
